package com.yy.yyui.text
{
	import com.siku.manor.view.common.UM;
	import com.siku.manor.view.component.GT;
	import com.siku.manor.view.component.IDataContainer;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextFieldType;
	import flash.text.TextFormatAlign;
	import flash.utils.Timer;
	
	import gs.*;
	import gs.easing.*;

	public class ArrowNumberInput extends Sprite implements IDataContainer
	{
		private var w:Number,h:Number;		
		
		public var minButton:SimpleButton;
		public var minusButton:SimpleButton;
		public var addButton:SimpleButton;
		public var maxButton:SimpleButton;
		public var numberInput:TextInput;
		private var _max:Number;
		private var _min:Number;
		private var _data:Object;
//		private var _value:RangeNumber;
		private var _actions:Object;
		private var _enabled:Boolean;
		
		public function ArrowNumberInput()
		{
			//TODO: implement function
			super();
			
			var arr:Array = GT.averageSlice(new ArrowNumberInputSkin(),4);
			//skin.dispose();
			var btn:SimpleButton;
			//four arrow buttons
			btn = UM.createButton(arr[0][0]);
			btn.y = 1;		
			this.addChild(GT.enableMouseDownAlphaEffect(btn));
			this.minButton = btn;

			btn = UM.createButton(arr[0][1]);
			btn.x = this.minButton.x+this.minButton.width;
			btn.y = this.minButton.y;
			this.addChild(GT.enableMouseDownAlphaEffect(btn));
			this.minusButton = btn;
			
			var ti:TextInput;
			ti = new TextInput();
			ti.x = this.minusButton.x+this.minusButton.width+1;
			ti.y = 0;
			ti.textLabel.textFormat.align = TextFormatAlign.CENTER;
			ti.textLabel.restrict = "0-9";
			ti.width = 50;
			this.addChild(ti);
			this.numberInput = ti;
			
			
			btn = UM.createButton(arr[0][2]);		
			btn.x = this.numberInput.x+this.numberInput.width+1;
			btn.y = this.minusButton.y;
			this.addChild(GT.enableMouseDownAlphaEffect(btn));
			this.addButton = btn;
			
			btn = UM.createButton(arr[0][3]);
			btn.x = this.addButton.x+this.addButton.width;
			btn.y = this.addButton.y;
			this.addChild(GT.enableMouseDownAlphaEffect(btn));
			this.maxButton = btn;
			
			w = this.maxButton.x+this.maxButton.width;
			h = this.maxButton.height;
			
//			this._value = new RangeNumber();
			
			//set actions
			this.addEventListener(MouseEvent.CLICK,clickHandler);
			this.addEventListener(KeyboardEvent.KEY_UP,inputHandler);
			this._actions = {};
			this.onClickLeftDoubleArrow = this.defaultOnClickLeftDoubleArrow;
			this.onClickLeftArrow = this.defualtOnClickLeftArrow;
			this.onClickRightArrow = this.defualtOnClickRightArrow;
			this.onClickRightDoubleArrow = this.defaultOnClickRightDoubleArrow;
			this.addEventListener(Event.REMOVED_FROM_STAGE,destroy);
		}
		
		public function destroy(...rest):void{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
			GT.destroyActions(this.actions);		
		}
		
		
		public function set enabled(value:Boolean):void{
			this._enabled = value;
			this.numberInput.textLabel.type = value?TextFieldType.INPUT:TextFieldType.DYNAMIC;
		}
		public function get enabled():Boolean{return _enabled;}
		
		private function clickHandler(e:MouseEvent):void{
			if(!enabled) return ;
			switch(e.target){
				case this.minButton:
					minButtonClickHandler(e);
					break;
				case this.minusButton:
					minusButtonClickHandler(e);
					break;
				case this.addButton:
					addButtonClickHandler(e);
					break;
				case this.maxButton:
					maxButtonClickHandler(e);
					break;
			}
		}
		
		private function inputHandler(e:KeyboardEvent):void{
			if(!enabled) return;
			if(e.target==this.numberInput.input){
				this.onChange(e);
			}
		}
		
		public function set onChange(value:Function):void{
			this.actions.onChange = value;
		}
		
		public function get onChange():Function{
			return this.actions.onChange;
		}
		
		
		//default input change
		public function defaultInputChange(e:Event):void{
			
			this.onChange(e);
		}
		
		//click right max
		public function get onClickRightDoubleArrow():Function{
			return this.actions.onClickRightDoubleArrow;
		}
		
		public function set onClickRightDoubleArrow(value:Function):void{
			this.actions.onClickRightDoubleArrow = value;
		}
		
		private function maxButtonClickHandler(e:MouseEvent):void{
			this.onClickRightDoubleArrow(e);
			this.onChange(e);
		}
		
		public function defaultOnClickRightDoubleArrow(e:MouseEvent):void{
			this.value = 0x7fffffff;
		}
		///////////////////////////
		
		//click right add
		public function get onClickRightArrow():Function{
			return this.actions.onClickRightArrow;
		}
		
		public function set onClickRightArrow(value:Function):void{
			this.actions.onClickRightArrow = value;
		}
		
		private function addButtonClickHandler(e:MouseEvent):void{
			this.onClickRightArrow(e);
			this.onChange(e);
		}
		
		private var clock:Timer = new Timer(1,400);
		private var oldv:Number;
		private var dest:Number;
		
		public function defualtOnClickRightArrow(e:MouseEvent):void{
//			clock.reset();
//			clock.start();
//			clock.addEventListener(TimerEvent.TIMER,add);
//			oldv = this.value;
			this.value++;
		}
		
		public function add(e:TimerEvent):void{
			
			this.value = int(Expo.easeIn(clock.currentCount*1,oldv,1000,clock.repeatCount*1));

		}
		////////////////////////////////
		
		//click left minus
		public function get onClickLeftArrow():Function{
			return this.actions.onClickLeftArrow;
		}
		
		public function set onClickLeftArrow(value:Function):void{
			this.actions.onClickLeftArrow = value;
		} 

		private function minusButtonClickHandler(e:MouseEvent):void{
			this.onClickLeftArrow(e);
			this.onChange(e);
		}
		
		public function defualtOnClickLeftArrow(e:MouseEvent):void{
			this.value--;			
		}
		/////////////////////////////////
				
		//click left min
		public function set onClickLeftDoubleArrow(value:Function):void{
			this.actions.onClickLeftDoubleArrow = value;
		}
		
		public function get onClickLeftDoubleArrow():Function{
			return this.actions.onClickLeftDoubleArrow;
		}

		private function minButtonClickHandler(e:MouseEvent):void{
			this.onClickLeftDoubleArrow(e);
			this.onChange(e);
		}
		
		public function defaultOnClickLeftDoubleArrow(e:MouseEvent):void{
			this.value = -0x7fffffff;
		}
		////////////////////////////
		
		public function get actions():Object{
			return _actions;
		}
		
		public function get value():Number{
			return Number(this.numberInput.text);
		}
		
		public function set value(newValue:Number):void{
//			this._value.value = newValue;			
//			this.numberInput.text = this._value.toString();
			this.numberInput.text = newValue.toString();
		}
		
//		public function get min():Number{
//			return _value.min;
//		}
//		
//		public function set min(value:Number):void{
//			this._value.min = value;
//		}
//		
//		public function get max():Number{
//			return _value.max;
//		}
//		
//		public function set max(value:Number):void{
//			this._value.max = value;
//		}

		public function get data():Object{return _data;}
		public function set data(value:Object):void{this._data = value;}
		
		override public function get width():Number{
			return w;
		}
		
		override public function get height():Number{
			return h;
		}
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			this.numberInput.width += diff;
			this.addButton.x += diff;
			this.maxButton.x += diff;
			w = newWidth;
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			h = newHeight;
		}
		
	}
}